<?php

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');

function presentVideos($videoArray) {
	foreach ($videoArray as $videoUrl) {
		echo '<object width="415" height="355">' . "\n";
		echo '<param name="movie" value="' . $videoUrl . '?version=3&autohide=1&showinfo=0' . '"></param>' . "\n";
		echo '<param name="allowScriptAccess" value="always"></param>';
		echo '<embed src="' . $videoUrl . '?version=3&autohide=1&showinfo=0" type="application/x-shockwave-flash" allowscriptaccess="always" width="425" height="355"></embed>' . "\n";
		echo '</object>' . "\n";
	}
}

function returnVideosByKeywords($searchTerms) {
	$termArray = explode(" ", $searchTerms);
	$searchTerms = implode("/", $termArray);

	$youtubedata = new Zend_Gdata_YouTube(); 
	$query = $youtubedata->newVideoQuery();
	$query->setOrderBy('viewCount');
	$query->setRacy('include');
	$query->setCategory($searchTerms);

	$videoFeed = $youtubedata->getVideoFeed($query);
	$videoArray = array();

	foreach($videoFeed as $videoEntry) {
		$flashplayerUrl = $videoEntry->getFlashPlayerUrl();
		$videoUrl = substr($flashplayerUrl, 0, strpos($flashplayerUrl, "?"));
		array_push($videoArray, $videoUrl);
	}

	return $videoArray;
}

//presentVideos(returnVideosByKeywords($_GET["searchquery"]));

?>
